/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package struts.actions;

import business.Document;
import business.IndexItemSimulitude;
import business.MiStringTokenizer;
import business.SM;
import com.opensymphony.xwork2.ActionSupport;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 *
 * @author Eduardo
 */
public class AdminAction extends ActionSupport {

    private SM sm = new SM();
    private String filePath;    //Indica la ruta donde buscar el fichero a indexar
    private String searchText;
    private List searchResult;

    public AdminAction(){}
   

    public String addFile(String fileName, String path)
    {
        this.sm.addFile(fileName, path);
        return SUCCESS;
    }

    public List search(String text)
    {
        return this.sm.search(text);
    }

    public void search()
    {
        searchResult = this.sm.search(searchText);
    }

    public String getCleanQuery(String searchWord)
    {
        String text = "";
        ArrayList<String> cleanQuery = this.sm.getCleanQuery(searchWord);
        for(String s : cleanQuery)
            text += s + " ";
        text = text.substring(0, text.length() - 1);        
        return text;
    }

    public String indexToString()
    {
        return this.sm.indexToString();
    }

    public boolean isEmpty()
    {
        return this.sm.isEmpty();
    }

    public String getFileName(int fileId)
    {
        return sm.getFileName(fileId);
    }

    public String getDocumentText(Document doc)
    {        
        return sm.getDocumentText(doc);
    }

    public void saveIndex()
    {        
        this.sm.saveIndex();
    }

    public void loadIndex()
    {
        this.sm.loadIndex();
    }

    public void generatedRel()
    {
        FileWriter fileRel = null;

        try {
            String namePROP = SM.class.getSimpleName().toLowerCase() + ".properties";
            Properties props = new Properties();
            props.load(SM.class.getResourceAsStream(namePROP));
            String path = props.getProperty("path");

            //Creamos un fichero para la salida (el .REL)
            fileRel = new FileWriter(path + "\\MED.REL");

            //Leemos el fichero .QRY y lo metemos en un string
            InputStream in = new BufferedInputStream(new FileInputStream(path + "\\MED.QRY"));
            InputStreamReader reader = new InputStreamReader(in);
            char[] buffer = new char[400];
            int readChars = 0;
            StringBuffer sBuffer = new StringBuffer(400);


            while(reader.ready())
            {
                readChars = reader.read(buffer);
                if(readChars>0)
                    sBuffer.append(buffer, 0, readChars);
            }
            String text = sBuffer.toString();
            reader.close();
            in.close();

            //Parseamos el string con el contenido del fichero
            MiStringTokenizer st = new MiStringTokenizer(text, ".I");
            st.nextToken();

            int idQuery = 1;
            while(st.hasMoreTokens())
            {
                String sText = st.nextToken();

                //Busco el .W y leo la query
                String query = sText.substring(sText.indexOf(".W") + 4, sText.length()).trim();

                //Buscamos y mostramos los resultado en el fichero .REL
                searchResult = this.search(query.toLowerCase());
                if(searchResult != null)
                {
                    for(int i=0; i<searchResult.size(); i++)
                    {
                        IndexItemSimulitude is = (IndexItemSimulitude)searchResult.get(i);
                        int fileId = is.getIndexItem().getDocReference();
                        //Redondeamos a 2 decimales
                        double sim = Math.round(is.getSimilitude() * Math.pow(10,2))/Math.pow(10,2);
                        fileRel.write(idQuery + " 0 " + fileId + " 1 " +  sim + " SMRI\n");
                    }                    
                }
                idQuery++;
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileRel.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }




    public SM getSm() {
        return sm;
    }

    public void setSm(SM sm) {
        this.sm = sm;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getSearchText() {
        return searchText;
    }

    public void setSearchText(String searchText) {
        this.searchText = searchText;
    }

    public List getSearchResult() {
        return searchResult;
    }

    public void setSearchResult(List searchResult) {
        this.searchResult = searchResult;
    }

}
